package com.maxmlr.music.song.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.maxmlr.music.admin.service.UserService;
import com.maxmlr.music.common.base.BaseServiceImpl;
import com.maxmlr.music.common.exception.XmlrException;
import com.maxmlr.music.song.dao.SpecialMapper;
import com.maxmlr.music.song.entity.Special;
import com.maxmlr.music.song.param.SpecialInfoParam;
import com.maxmlr.music.song.service.SpecialService;
import com.maxmlr.music.song.service.transfer.SpecialTransfer;
import com.maxmlr.music.system.service.FilesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author max
 * @version 1.0
 * @ClassName SpecialServiceImpl
 * @Description 专辑记录表  服务实现类
 * @Copyright Copyright (c) 2020: www.maxmlr.com.
 * @since 2020-06-07
 */
@Slf4j
@Service
public class SpecialServiceImpl extends BaseServiceImpl<SpecialMapper, Special> implements SpecialService {

    @Autowired
    private SpecialMapper specialMapper;

    @Autowired
    private FilesService filesService;

    @Autowired
    private UserService userService;

    @Autowired
    private SpecialTransfer specialTransfer;

    @Override
    public Page<SpecialInfoParam> listSpecialInfo(Page<SpecialInfoParam> page) {
        specialMapper.selectSpecialInfo(page);
        filesService.getFilesByIds(page.getRecords(), SpecialInfoParam::getHeadImgId, SpecialInfoParam::setHeadImg);
        return page;
    }

    @Override
    public boolean saveParam(SpecialInfoParam param) {
        Special one = getOne(Wrappers.<Special>lambdaQuery()
                .eq(Special::getSpecialName, param.getSpecialName())
                .eq(Special::getSingerId, param.getSingerId()));
        if (Objects.nonNull(one)) {
            throw new XmlrException("专辑名重复");
        }
        Special special = specialTransfer.po2Special(param, userService.getAdminUser());
        return save(special);
    }

    @Override
    public boolean updateParamById(SpecialInfoParam param) {
        Special special = getById(param.getId());
        if (Objects.isNull(special)) {
            throw new XmlrException("专辑信息不存在");
        }
        specialTransfer.fullSpecial(special, param, userService.getAdminUser());
        return updateById(special);
    }

    @Override
    public Object transferParam(Special special) {
        SpecialInfoParam param = specialTransfer.po2SpecialInfoParam(special);
        filesService.getFileById(param, SpecialInfoParam::getHeadImgId, SpecialInfoParam::setHeadImg);
        return param;
    }

    @Override
    public List<SpecialInfoParam> transferParams(List<Special> list) {
        List<SpecialInfoParam> params = list.stream().map(specialTransfer::po2SpecialInfoParam).collect(Collectors.toList());
        filesService.getFilesByIds(params, SpecialInfoParam::getHeadImgId, SpecialInfoParam::setHeadImg);
        return params;
    }
}
